﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Kawanon.Usability;

namespace Kawanon.QtHoney
{
	class GesturePanel : GroupBox
	{
		private const uint WM_RBUTTONDOWN = 0x0204;
		private const uint WM_RBUTTONUP = 0x0205;

		private GestureProvider m_gesture = new GestureProvider();

		public event GestureEventHandler GestureDetected;
		public event GestureEventHandler TrailDetected;
		public event MouseEventHandler MaybeGestureStarted;

		public GesturePanel()
		{
			m_gesture.AllowUnknownExpression = true;
			m_gesture.GestureDetected += new GestureEventHandler(GestureProvider_GestureDetected);
			m_gesture.TrailDetected += new GestureEventHandler(GestureProvider_TrailDetected);
			m_gesture.Expressions = GestureSettings.LoadAvailableExpressions();
		}

		void GestureProvider_TrailDetected(object sender, GestureEventArgs e)
		{
			if (TrailDetected != null)
			{
				TrailDetected.Invoke(this, e);
			}
		}

		void GestureProvider_GestureDetected(object sender, GestureEventArgs e)
		{
			if (GestureDetected != null)
			{
				GestureDetected.Invoke(this, e);
			}
		}

		protected override void WndProc(ref Message m)
		{
			if (m.Msg == WM_RBUTTONDOWN)
			{
				if (MaybeGestureStarted != null)
				{
					MouseEventArgs e = new MouseEventArgs(MouseButtons.Right, 1,
						Cursor.Position.X, Cursor.Position.Y, 0);

					MaybeGestureStarted.Invoke(this, e);
				}
			}

			if (m_gesture.ProcessMessage(ref m))
			{
				return;
			}
			base.WndProc(ref m);
		}
	}
}
